package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import com.module.commonui.R;
import com.ns.yc.yccardviewlib.shadow.ShadowLayout;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;



public class PtzDirectionView extends PtzView implements View.OnClickListener, View.OnTouchListener, View.OnLongClickListener {
    public static final String TAG = PtzDirectionView.class.getSimpleName();
    private int mOrientation = 0;
    private RelativeLayout relativeLayout;
    private LinearLayout linearLayout;
    private ShadowLayout shadowLayout;


    public int getOrientation(){
        return mOrientation;
    }

    private void setImageLandscape(){
        relativeLayout.setBackgroundResource(R.color.transparent);
        linearLayout.setBackgroundResource(R.drawable.audio_chat_btn_land_background);
        shadowLayout.setShadowColor(ResourcesUtil.getResources().getColor(R.color.transparent));


    }

    private void setImageNormal(){
        relativeLayout.setBackgroundResource(R.color.camera_content_new_bg);
        linearLayout.setBackgroundResource(R.drawable.audio_chat_btn_background);
        shadowLayout.setShadowColor(ResourcesUtil.getResources().getColor(R.color.white_rounded_btn_shadow_color));

    }

    public void setLandscape(boolean landscape) {
        mIsLandscape = landscape;
        if (landscape){
            setImageLandscape();
        }else{
            setImageNormal();
        }
    }


    public PtzDirectionView(Context context) {
        super(context);
        initContentView(context);
    }

    public PtzDirectionView(Context context, int orientation) {
        super(context);
        this.mOrientation = orientation;

        initContentView(context);
    }

    public PtzDirectionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PtzDirectionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }


    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PtzDirectionView);

        mOrientation = typedArray.getInt(R.styleable.PtzDirectionView_ptz_orientation, 0);
        typedArray.recycle();
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        int id = view.getId();
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){


            if (view instanceof ImageView){
                int resId = R.drawable.ic_global_press_arrow_left;
                int resId2 = R.drawable.ptz_background_left_hor;

                if (mIsLandscape){
                     resId = R.drawable.ic_global_press_arrow_left;
                     resId2 = R.drawable.ptz_background_press_left_hor;
                    if (id == R.id.iv_arrow_first){
                        resId = mOrientation == 0? resId : R.drawable.ic_global_press_arrow_up;
                        resId2 = mOrientation == 0? resId2 : R.drawable.ptz_background_press_up_ver;
                    }else if (id == R.id.iv_arrow_second){
                        resId = mOrientation == 0? R.drawable.ic_global_press_arrow_right: R.drawable.ic_global_press_arrow_bottom;
                        resId2 = mOrientation == 0? R.drawable.ptz_background_press_right_hor : R.drawable.ptz_background_press_down_ver;
                    }
                }else{
                    if (id == R.id.iv_arrow_first){
                        resId = mOrientation == 0? resId : R.drawable.ic_global_press_arrow_up;
                        resId2 = mOrientation == 0? resId2 : R.drawable.ptz_background_up_ver;
                    }else if (id == R.id.iv_arrow_second){
                        resId = mOrientation == 0? R.drawable.ic_global_press_arrow_right: R.drawable.ic_global_press_arrow_bottom;
                        resId2 = mOrientation == 0? R.drawable.ptz_background_right_hor : R.drawable.ptz_background_down_ver;
                    }
                }

                ((ImageView)view).setImageResource(resId);
                view.setBackgroundResource(resId2);
            }

        }else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
            if (mIsLandscape){
                if (view instanceof ImageView){
                    setImageLandscape();
                }
            }else{
                relativeLayout.setBackgroundResource(R.color.camera_content_new_bg);
                linearLayout.setBackgroundResource(R.drawable.audio_chat_btn_background);
                shadowLayout.setShadowColor(ResourcesUtil.getResources().getColor(R.color.white_rounded_btn_shadow_color));
                if (view instanceof ImageView){
                    int resId = R.drawable.ic_arrow_left;
                    if (id == R.id.iv_arrow_first){
                        resId = mOrientation == 0? resId : R.drawable.ic_arrow_top;
                    }else if (id == R.id.iv_arrow_second){
                        resId = mOrientation == 0? R.drawable.ic_arrow_right: R.drawable.ic_arrow_bottom;
                    }
                    ((ImageView)view).setImageResource(resId);
                    view.setBackgroundResource(R.color.transparent);
                }
            }

        }
        return false;
    }


    @Override
    public boolean onLongClick(View view) {
        TGLog.d(TAG, "onLongClick");
        int id = view.getId();
        TGLog.d(TAG, "onClick id = " + id);
        if (id == R.id.iv_arrow_first){
            sendLongCmd(mOrientation == 0? PtzView.AVIOCTRL_PTZ_LEFT : PtzView.AVIOCTRL_PTZ_UP);
        }else if (id == R.id.iv_arrow_second){
            sendLongCmd(mOrientation == 0? PtzView.AVIOCTRL_PTZ_RIGHT : PtzView.AVIOCTRL_PTZ_DOWN);
        }

        return false;
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initContentView(Context context) {
        if (mOrientation == 0){
            View rootView = LayoutInflater.from(context).inflate(R.layout.layout_ptz_dir_hor, this, true);
            relativeLayout = rootView.findViewById(R.id.rl_bkg);
            linearLayout = rootView.findViewById(R.id.ll_ptz_layout);
            shadowLayout = rootView.findViewById(R.id.shadow_shadow_layout);


        }else{
            View rootView = LayoutInflater.from(context).inflate(R.layout.layout_ptz_dir_ver, this, true);
            shadowLayout = rootView.findViewById(R.id.shadow_shadow_layout);
            relativeLayout = rootView.findViewById(R.id.rl_bkg);
            linearLayout = rootView.findViewById(R.id.ll_ptz_layout);

        }

        relativeLayout.setGravity(Gravity.CENTER);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        TGLog.d(TAG, "onClick id = " + id);
        if (id == R.id.iv_arrow_first){
            sendShortCmd(mOrientation == 0? PtzView.AVIOCTRL_PTZ_LEFT : PtzView.AVIOCTRL_PTZ_UP);
        }else if (id == R.id.iv_arrow_second){
            sendShortCmd(mOrientation == 0? PtzView.AVIOCTRL_PTZ_RIGHT : PtzView.AVIOCTRL_PTZ_DOWN);
        }
    }

    private void sendShortCmd(int cmd) {
        if (onPtzControlTouchListener != null) {
            if (isSendPTZCmd) {
                TGLog.d(PtzDirectionView.TAG, "onStopCmd ptzCmd");
                onPtzControlTouchListener.onStopCmd();
            } else {
                TGLog.d(PtzDirectionView.TAG, "onShortCmd111 ptzCmd =" + cmd);
                onPtzControlTouchListener.onShortCmd(cmd);
            }
            isSendPTZCmd = false;
            TGLog.d("PtzControlView", "sendShortCmd = " + cmd);
            ptzCmd = -1;
        }
    }

    private void sendLongCmd(int cmd) {
        if (onPtzControlTouchListener != null) {
            if (ptzCmd == -1) {
                ptzCmd = cmd;
                TGLog.d(PtzDirectionView.TAG, "onLongCmd cmd =" + cmd);
                onPtzControlTouchListener.onLongCmd(ptzCmd);
            } else if (cmd != ptzCmd) {
                ptzCmd = cmd;
                TGLog.d(PtzDirectionView.TAG, "onLongCmd2 cmd =" + cmd);
                onPtzControlTouchListener.onStopCmd();
                onPtzControlTouchListener.onLongCmd(ptzCmd);
            }
        }
    }
}
